<template>
  <div>
    <basic-modal :modalOpt="modal" :footer="null" @closeModal="closeModal">
      <template>
        <basic-form
          :formInfos="s_FormInfo"
          :formDatas="s_formDatas"
          :rules="s_rules"
          @onSubmit="onSubmit"
          @onChangeType="onChangeType"
          @closeModal="closeModal"
        ></basic-form>
      </template>
    </basic-modal>
  </div>
</template>

<script>
import basicModal from '@/views/app/biz/common/basicModal'
import basicForm from '../common/basicForm'
import {getLicenseByPid} from '@/views/app/biz/regisLicense/regisLicense'
import {
  getInfoByPid,
  saveInfo
} from './salegoods'

const s_FormInfo = [
  { label: '注册证号', name: 'zhuCezhPid', type: 'zchselect', zhuangTai:'正常',  placeholder: '请选择注册证号' },
  { label: '产品销售代码', name: 'chanPingdm', type: 'input', placeholder: '请输入产品销售代码' },
  { label: '品名', name: 'pinMing', type: 'input', placeholder: '请输入品名' },
  { label: '通用名', name: 'tongYongm', type: 'input', placeholder: '请输入通用名' },
  { label: '规格', name: 'guiGe', type: 'input', placeholder: '请输入' },
  { label: '型号', name: 'xingHao', type: 'input', placeholder: '请输入' },
  { label: '品牌', name: 'pingPai', type: 'input', placeholder: '请输入' },
  { label: '一级分类', name: 'yiJifldm', type: 'input', placeholder: '请输入一级分类代码' },
  // { label: '一级分类名称', name: 'yiJiflmc', type: 'input', placeholder: '请输入' },
  { label: '二级分类代码', name: 'erJifldm', type: 'input', placeholder: '请输入' },
  { label: '二级分类名称', name: 'erJiflmc', type: 'input', placeholder: '请输入' },
  { label: '三级分类代码', name: 'sanJifldm', type: 'input', placeholder: '请输入' },
  { label: '医保代码', name: 'yiBaodm', type: 'input', placeholder: '请输入' },
  { label: '计量单位', name: 'jiLiangdw', type: 'input', placeholder: '请输入' },
  { label: '无税单价(元)', name: 'wuShuidj', type: 'number', placeholder: '请输入' },
  { label: '含税单价(元)', name: 'hanShuidj', type: 'number', placeholder: '请输入' },
  { label: '税率（百分比）', name: 'shuiLv', type: 'number', placeholder: '请输入' },
  { label: '生产企业信用代码', name: 'shengChanqydm', type: 'input', placeholder: '请输入' },
  { label: '生产企业名称', name: 'shengChanqymc', type: 'input', placeholder: '请输入' },
  { label: '产地', name: 'chanDi', type: 'input', placeholder: '请输入' },
  { label: '价值属性', name: 'jiaZhisx', type: 'select', placeholder: '请输入' ,
    options:[{id:'高值',value:'高值'},{id:'低值',value:'低值'}]},
  { label: '大包装数', name: 'daBaozs', type: 'number', placeholder: '请输入' },
  { label: '中包装数', name: 'zhongBaozs', type: 'number', placeholder: '请输入' },
  { label: '小包装数', name: 'xiaoBaozs', type: 'number', placeholder: '请输入' },
  { label: '最小采购量', name: 'zuiXiaocgl', type: 'number', placeholder: '请输入' },
  { label: ['保存', '重置'], type: 'botton' },
]
const s_formDatas = {
  sysSeq: 0,
  pid: '',
  yiJifldm: null,
  yiJiflmc: null,
  erJifldm: null,
  erJiflmc: null,
  sanJifldm: null,
  sanJiflmc: null,
  yiBaodm: null,
  chanPingdm: null,
  pinMing: null,
  tongYongm: null,
  guiGe: null,
  xingHao: null,
  shuoMing: null,
  pingPai: null,
  jiLiangdw: null,
  wuShuidj: null,
  hanShuidj: null,
  shuiLv: null,
  shengChanqyPid: null,
  shengChanqymc: null,
  shengChanqydm: null,
  zhuCezhPid: null,
  zhuCezh: null,
  chanDi: null,
  jiaZhisx: null,
  daBaozs: null,
  zhongBaozs: null,
  xiaoBaozs: null,
  zuiXiaocgl: null,
  zhuangTai: null,
  zhaoPian1: null,
  zhaoPian2: null,
  zhaoPian3: null,
}
const s_rules = {
  pinMing: [{ required: true, message: '品名必填', trigger: 'blur' }],
  guiGe: [{ required: true, message: '规格必填，没有请填“无”', trigger: 'blur' }],
  chanPingdm: [{ required: true, message: '产品代码必填', trigger: 'change' }],
  hanShuidj: [{ required: true, message: '含税单价必填', trigger: 'change' }],
}
export default {
  components: {
    basicModal,
    basicForm
  },
  data() {
    return {
      loading: false,
      modal: {
        modalVisible: false,
        modalTitle: '新增品种',
        modalWidth: '1000px',
      },
      s_FormInfo: s_FormInfo,
      s_formDatas: s_formDatas,
      s_rules: s_rules
    }
  },
  computed: {},
  mounted() {
  },
  methods: {
    // 新增按钮 点击了新增
    addModal() {
      this.s_formDatas ={
        sysSeq: 0,
        pid: '',
        yiJifldm: null,
        yiJiflmc: null,
        erJifldm: null,
        erJiflmc: null,
        sanJifldm: null,
        sanJiflmc: null,
        yiBaodm: null,
        chanPingdm: null,
        pinMing: null,
        tongYongm: null,
        guiGe: null,
        xingHao: null,
        shuoMing: null,
        pingPai: null,
        jiLiangdw: null,
        wuShuidj: null,
        hanShuidj: null,
        shuiLv: null,
        shengChanqyPid: null,
        shengChanqymc: null,
        shengChanqydm: null,
        zhuCezhPid: null,
        zhuCezh: null,
        chanDi: null,
        jiaZhisx: undefined,
        daBaozs: null,
        zhongBaozs: null,
        xiaoBaozs: null,
        zuiXiaocgl: null,
        zhuangTai: null,
        zhaoPian1: null,
        zhaoPian2: null,
        zhaoPian3: null
      }
      console.log('点击了新增')
      this.modal.modalVisible = true
      this.modal.modalTitle = "新增品种"
    },
    // 新增按钮 点击了新增
    editModal(val) {
      console.log('点击了编辑')
      this.getSBypid(val)
      this.modal.modalVisible = true
      this.modal.modalTitle = "修改品种"
    },
    // 关闭模态详细框
    closeModal() {
      this.modal.modalVisible = false
      this.$emit('close')
    },
    // 保存成功关闭模态详细框
    closeModal2() {
      this.modal.modalVisible = false
      this.$emit('ok')
    },
    // 模态详细框提交按钮
    onSubmit(val) {
      this.saveObjInfo(val)
    },
    // 保存详细信息
    async saveObjInfo(val) {
      this.loading = true
      // this.$message.loading({ content: '提交中'， })
      let res = await saveInfo(val)
      this.loading = false
      console.log(res)
      if (res && res.code && res.code === 'C1' && res.data) {
        this.$message.success({ content: '提交成功',  duration: 2 })
        this.closeModal2()
      } else {
        this.$message.error({ content: '提交失败',  duration: 2 })
      }
    },
    // 得到初始化信息
    async getSBypid(val) {
      let res = await getInfoByPid(val)
      if (res && res.code && res.code === 'C1') {
        if (res.data) {
          this.s_formDatas = res.data
        } else {
          this.addModal()
        }
      }
      console.log(res)
    },
    // 表单编号
    onChangeType(name, val) {
      console.log('formName',name)
      console.log('val',val)
      switch (name) {
        case 'zhuCezhPid': 
        // console.log('aaaaaaaaaaaaaa',name)
        const zhucez = this.getZhuCezxx(val)
        break
      }
    },
    // 获取注册证详细信息
    async getZhuCezxx (pid) {
      const res = await getLicenseByPid(pid)
       if (res && res.code && res.code === 'C1' && res.data) {
        this.s_formDatas = Object.assign( this.s_formDatas,  {zhuCezhPid: res.data.pid })
        this.s_formDatas = Object.assign( this.s_formDatas,  {zhuCezh: res.data.zhengZhaohm })
        this.s_formDatas = Object.assign( this.s_formDatas,  {shengChanqymc: res.data.shengChancsmc })
        this.s_formDatas = Object.assign( this.s_formDatas,  {pinMing: res.data.chanPinmc })
       }
    },
    //
    restData (val) {
       this.getSBypid(this.s_formDatas.pid)
    }
  },
}
</script>

<style>
</style>